import { BUTTON_STYLES, InsertPosition } from '../../../types/constants'
import { ToolKit } from '../../utils/toolkit'

export class PageButtonHandler {
  private static instance: PageButtonHandler

  /**
   * 获取单例实例
   */
  public static getInstance(): PageButtonHandler {
    if (!PageButtonHandler.instance) {
      PageButtonHandler.instance = new PageButtonHandler()
    }
    return PageButtonHandler.instance
  }

  /**
   * 添加图标
   * @param $element 目标元素
   * @param options 选项
   * @param options.id 按钮 id
   * @param options.content 按钮内容
   * @param options.classNames 按钮样式类名
   * @param options.position 插入位置
   */
  addButton(
    $element: JQuery,
    options: {
      id: string
      content: string
      classNames?: string
      position?: InsertPosition
    },
  ): JQuery {
    const {
      id,
      content,
      classNames = BUTTON_STYLES.secondary.className,
      position = InsertPosition.Append,
    } = options || {}

    const $button = $(`
      <button class="btn ${classNames} ${id}-btn">
        <span class="${id}-icon"></span>${content}
      </button>
    `)

    ToolKit.dom.insertElement($element, $button, position)

    return $button
  }
}
